<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleRecommendModel;
use Illuminate\Http\Request;

class RecommendListResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "redirectUrl"     => self::getRedirectUrl($this->contentType, $this->articleId, $this->redirectUrl),
            "type"            => $this->type,
            "typeText"        => translateEnumCodeToCn(ArticleRecommendModel::TYPE_TEXT, $this->type, ""),
            "description"     => $this->description,
            "cover"           => $this->cover,
            "sort"            => $this->sort,
            "title"           => $this->title,
            "articleId"       => $this->articleId,
            "isActivate"      => $this->isActivate,
            "isActivateText"  => translateEnumCodeToCn(ArticleRecommendModel::ACTIVATE_TEXT, $this->isActivate, ""),
            "contentType"     => $this->contentType,
            "contentTypeText" => translateEnumCodeToCn(
                ArticleRecommendModel::CONTENT_TYPE_TEXT,
                $this->contentType,
                ""
            ),
            'article'         =>
                $this->articleRel && $this->contentType == ArticleRecommendModel::CONTENT_TYPE_ARTICLE ?
                    ApiArticleListResource::collection([$this->articleRel])->getIterator()->current() : null
        ];
    }

    /**
     * 获取跳转地址
     *
     * @param int $contentType
     * @param int $articleId
     * @param string $oldRedirectUrl
     * @return string
     */
    public static function getRedirectUrl(int $contentType, int $articleId, string $oldRedirectUrl = ''): string
    {
        $redirectUrl = '';
        if ($contentType == ArticleRecommendModel::CONTENT_TYPE_CATEGORY) {
            $redirectUrl = config('open-shop.m') . "content/subject/" . $articleId . "?hideAppBar=true";
        } elseif ($contentType == ArticleRecommendModel::CONTENT_TYPE_REDIRECT_URL) {
            $redirectUrl = $oldRedirectUrl ?? '';
        }

        return $redirectUrl;
    }
}
